package org.example.cake.controller;

import org.example.cake.entity.OrderDetail;
import org.example.cake.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:订单详情控制层
 * @Author: luojiangjin
 * @Date: 2025/8/9 12:12
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/orderdetail")
public class OrderDetailController {
    @Autowired
    OrderDetailService orderDetailService;

    /*@GetMapping("/{orderId}")
    public ResponseEntity<Map<String, Object>> getOrderDetail(@PathVariable Integer orderId) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 参数验证
            if (orderId == null || orderId <= 0) {
                response.put("code", 400);
                response.put("message", "订单ID必须为正整数");
                return ResponseEntity.badRequest().body(response);
            }

            // 查询订单详情
            OrderDetail orderDetail = orderDetailService.getOrderDetailById(orderId);
            if (orderDetail == null) {
                response.put("code", 404);
                response.put("message", "订单不存在");
                return ResponseEntity.ok(response);
            }

            response.put("code", 200);
            response.put("message", "获取订单详情成功");
            response.put("data", orderDetail);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("code", 500);
            // 返回具体错误信息（开发环境），生产环境可简化
            response.put("message", "服务器内部错误：" + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }*/

    @GetMapping("/{orderId}")
    public ResponseEntity<Map<String, Object>> getOrderDetail(@PathVariable Integer orderId) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 参数验证
            if (orderId == null || orderId <= 0) {
                response.put("code", 400);
                response.put("message", "订单ID必须为正整数");
                return ResponseEntity.badRequest().body(response);
            }

            Map<String, Object> orderData = orderDetailService.getOrderWithCakesById(orderId);
            if (orderData == null) {
                response.put("code", 404);
                response.put("message", "订单不存在");
                return ResponseEntity.ok(response);
            }

            response.put("code", 200);
            response.put("message", "获取订单详情成功");
            response.put("data", orderData); // 此时data包含订单基础信息和cakesList数组
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("code", 500);
            response.put("message", "服务器内部错误：" + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
}


